package net.sxnic.ugr.dept;

/**
 * 
 * Introduction：部门实体类
 * 
 * @author 孙宇飞 create date : 2010-6-2
 * @author 吕盛槐 update date : 2011-9-25
 */
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import net.sxinfo.core.spring25.Dao;
import net.sxinfo.core.spring25.DefaultManager;
import net.sxnic.ugr.dept.dao.DeptDao;
import net.sxnic.ugr.user.User;
import net.sxnic.ugr.user.dao.UserDao;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("deptManager")
public class DefaultDeptManager extends DefaultManager<Dept, DeptDao, String>
		implements DeptManager {

	@Autowired
	private DeptDao deptDao;
	
	@Autowired
	private UserDao userDao;

	@Override
	protected Dao<Dept, String> getEntityDao() {
		return deptDao;
	}

	@Override
	public Map<String, String> findMap() {
		Map<String, String> map = new LinkedHashMap<String, String>();		

		List<Dept> list = deptDao.getAll();
		for (Dept dept : list) {
			map.put(dept.getCode(), dept.getName());
		}

		return map;
	}

	@Override
	public Dept findByCode(String code) {
		return deptDao.findByUnique("code", code);
	}

	@Override
	public List<Dept> findByUser(User user) {
		return deptDao.findByUser(user);
	}

	@Override
	public String findDeptCodesByUser(User user) {
		String temp = "";
		List<Dept> list = deptDao.findByUser(user);
		
		for(Dept d:list){
			if(d!=null){
				temp = temp+d.getCode()+",";
			}
		}
		
		return StringUtils.removeEnd(temp, ",");
	}

	@Override
	public List<Dept> findByUserName(String userName) {
		return findByUser(userDao.findByUnique("username",userName));
	}

	@Override
	public List<Dept> findPureFunctionDepart() {
		return deptDao.findBySql("from Dept where ctype=?  order by  deptOrder ",
				new Object[] { "02"  });
	}

	@Override
	public List<Dept> getTopList() {
		return deptDao.getTopList();
	}

	@Override
	public List<Dept> getChildList(String root) {
		return deptDao.getChildList(root);
	}
	
	/**
	 * 根据部门类别获取部门List
	 * @param ctype 部门类别
	 * @return 获取指定类型的部门list
	 */
	public List<Dept> getDeptByType(String ctype){
		return deptDao.getDeptByType(ctype);
	}

}
